package com.rocketmq.demo.batch;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * consumer
 */
public class Consumer {

    public static void main(String[] args) {
        //1.创建消费者consumer .并制定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");

        //2.指定nameservice地址
        consumer.setNamesrvAddr("192.168.31.67:9876;192.168.31.126:9876;192.168.31.209:9876");
        //3.订阅主题topic和tag
        try {
            consumer.subscribe("BatchTopic", "*");
            //4.设置回调函数处理信息
            consumer.registerMessageListener(new MessageListenerConcurrently() {
                //接收消息类容
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {

                    for (MessageExt msg : list) {
                        System.out.println("consumethread=" + Thread.currentThread().getName() + "," + new String(msg.getBody()));
                    }

                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            });

            //5.启动消费者consumer
            consumer.start();
            System.out.println("消费者启动");
        } catch (MQClientException e) {
            e.printStackTrace();
        }


    }

}
